package com.front.web.wx.utils;



import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.HttpUtil;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.wx.domain.Config;

public class MobanMessageUtils {
	
	
	/**
	 * 发送微信模板消息
	 * @param openId
	 * @param templateId
	 * @param firstData
	 * @param keyword1
	 * @param keyword2
	 * @param keyword3
	 * @param keyword4
	 * @param remark
	 * @param url
	 * @return
	 */
	private static String createWxMobanMessage(String openId,String templateId,
			String firstData,String keyword1,String keyword2,
			String keyword3,String keyword4,String remark,String url) {
		
		JSONObject json  = new JSONObject();
		json.put("touser", openId);
		json.put("template_id", templateId);
		if(!StringUtils.isEmpty(url)) {
			json.put("url", url);
		}
		JSONObject data  = new JSONObject();
		if(!StringUtils.isEmpty(firstData)) {
			JSONObject node  = new JSONObject();
			node.put("value", firstData);
			node.put("color", "#173177");
			data.put("first", node);
		}
		if(!StringUtils.isEmpty(keyword1)) {
			JSONObject node  = new JSONObject();
			node.put("value", keyword1);
			node.put("color", "#173177");
			data.put("keyword1", node);
		}
		if(!StringUtils.isEmpty(keyword2)) {
			JSONObject node  = new JSONObject();
			node.put("value", keyword2);
			node.put("color", "#173177");
			data.put("keyword2", node);
		}
		if(!StringUtils.isEmpty(keyword3)) {
			JSONObject node  = new JSONObject();
			node.put("value", keyword3);
			node.put("color", "#173177");
			data.put("keyword3", node);
		}
		if(!StringUtils.isEmpty(keyword4)) {
			JSONObject node  = new JSONObject();
			node.put("value", keyword4);
			node.put("color", "#173177");
			data.put("keyword4", node);
		}
		if(!StringUtils.isEmpty(remark)) {
			JSONObject node  = new JSONObject();
			node.put("value", remark);
			node.put("color", "#173177");
			data.put("remark", node);
		}
		json.put("data", data);
		return json.toString();
		
	}
	
	
	public static void sendWxMobanXaoxi(Config config,
			String openId,String templateId,
			String firstData,String keyword1,String keyword2,
			String keyword3,String keyword4,String remark,String url) throws Exception {
		
			String p = createWxMobanMessage(openId, templateId, firstData, keyword1, keyword2, keyword3,
					keyword4, remark, url);
			System.err.println("请求 - >"+p);
		
			String r =	HttpUtil.doPost("https:"
				+ "//api.weixin.qq.com/cgi-bin"
				+ "/message/template/send?access_token="+TokenUtils.getToken(config), p
				);
			System.err.println("发送模板消息 ->"+r);
	}
	
	
}
